/**
 Copyright (C) 2013 Yuyao Liang

 This source code is release under the BSD License.

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.  See the license for more details.

 Created on Nov 25, 2013 3:58:08 PM
 */
package com.googlecode.backtest.dataset.manager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.googlecode.backtest.rule.ISourceAlgorithm;

/**
 * @author lianyuy
 * 
 */
public abstract class DataManager implements IDataManager {
	protected Map<String, Object> map = new HashMap<String, Object>();
	protected Map<String, List<DataReadyEventListener>> listenermap = new HashMap<String, List<DataReadyEventListener>>();

	@SuppressWarnings("rawtypes")
	protected List<ISourceAlgorithm> sourcelist = new ArrayList<ISourceAlgorithm>();

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.googlecode.backtest.document.IDataManager#putData(java.lang.String,
	 * java.lang.Object)
	 */
	@Override
	public void putData(String key, Object data) {
		map.put(key, data);
		List<DataReadyEventListener> list = listenermap.get(key);
		if (list != null) {
			for (DataReadyEventListener listener : list) {
				listener.OnDataReady(new DataReadyEvent(this, key));
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.googlecode.backtest.document.IDataManager#getData(java.lang.String)
	 */
	@Override
	public Object getData(String key) {
		return map.get(key);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.googlecode.backtest.dataset.manager.IDataManager#register(java.lang
	 * .String, com.googlecode.backtest.dataset.manager.DataReadyEventListener)
	 */
	@Override
	public void register(String key, DataReadyEventListener listener) {
		if (listenermap.containsKey(key)) {
			List<DataReadyEventListener> list = listenermap.get(key);
			list.add(listener);
		} else {
			List<DataReadyEventListener> list = new ArrayList<DataReadyEventListener>();
			list.add(listener);
			listenermap.put(key, list);
		}
	}

	@SuppressWarnings("rawtypes")
	public List<ISourceAlgorithm> getSourceList() {
		return sourcelist;
	}

}
